# flickr_key d4aa294487e70a1211474cfb9c961e84
# secret 47418c8e227e6071

require 'open-uri'
require 'rexml/document'
module Flickr
  
  def small_photo_uri(photo)
    server = photo.attribute('server')
    id = photo.attribute('id')
    secret = photo.attribute('secret')
    return "http://static.flickr.com/#{server}/#{id}_#{secret}_m.jpg"
  end
  
  def grab_photo_uris(api_key, tag)
    uri= "http://www.flickr.com/services/rest?method=flickr.photos.search"+
       "&api_key=#{api_key}&tags=#{tag}&per_page=5&content_type=2"
    
    response = open(uri).read
    
    doc = REXML::Document.new(response)
    photos = []
    REXML::XPath.each(doc,'//photo') do |photo|
      photos << small_photo_uri(photo) if photo
    end
    return photos
  end
  
  
  
end
#if ARGV.size < 2
#  puts "Usage: #{$0} [Flickr API Key][search term]"
#  exit
#end
#api_key, tag = ARGV
#puts grab_photo_uris(api_key, tag).join("\n")